/*
 * @Author: xuetao 3168581151@qq.com
 * @Date: 2025-02-15
 * @LastEditors: xuetao 3168581151@qq.com
 * @LastEditTime: 2025-02-26
 * @Description: 
 */
import 'package:flutter/material.dart';

import 'package:chat_client/providers/individual_provider.dart';
import 'package:provider/provider.dart';

import '/widgets/custom_global_widgets.dart';
import 'asistant_page.dart';

// 用户信息修改页面
class MobileUserInfoPage extends StatelessWidget {
  const MobileUserInfoPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(title: const Text('个人信息')),
        body: Consumer<IndividualProvider>(builder: (context, model, child) {
          return ListView(
            children: [
              _bildAvatarUpdateItem('头像', model.avatarUrl,
                  () => model.pickImageToUpdate(), model),
              CListTile(
                title: '名字',
                onTap: () => model.navToWidget(UpdateByInput(
                  oldInfo: model.user.nickname,
                  type: 'nickname',
                  title: '修改名字',
                  describe: '阿弥诺斯',
                )),
                trailing: _buildTralling(text: model.user.nickname),
              ),
              Divider(height: 0),
              CListTile(
                  title: '生日',
                  trailing: _buildTralling(
                      text: model.getDateFormat(model.user.birthday)),
                  onTap: () => birthdayPicke(context: context, model: model)),
              Divider(height: 0),
              CListTile(
                title: 'echo账号',
                onTap: () => model.navToWidget(UpdateByInput(
                  oldInfo: model.user.username,
                  type: 'username',
                  title: '修改账号',
                  describe: '其他用户可通过账号搜索到自己',
                )),
                trailing: _buildTralling(text: model.user.username),
              ),
              Divider(height: 0),
              CListTile(
                title: '性别',
                trailing: _buildTralling(
                    text: model.getGenderLabel(model.user.gender)),
                onTap: () => model.navToWidget(UpdateByOption(
                  oldInfo: model.user.gender,
                  type: 'gender',
                  title: '修改性别',
                  widgetIndex: 1,
                )),
              ),
              Divider(height: 0),
              CListTile(
                title: '个性签名',
                onTap: () => model.navToWidget(UpdateByInput(
                  oldInfo: model.user.signature,
                  type: 'signature',
                  title: '修改个性签名',
                  describe: '阿弥诺斯',
                )),
                trailing: _buildTralling(text: model.user.signature),
              ),
              Divider(height: 0),
              CListTile(
                title: '地区',
                trailing: _buildTralling(text: model.user.region),
              ),
              DividerWithText(text: '以下信息不可修改'),
              CListTile(
                title: '手机号',
                trailing: _buildTralling(
                    text: model.user.phoneNumber, modified: false),
              ),
              Divider(height: 0),
              CListTile(
                title: '注册时间',
                trailing: _buildTralling(
                    text: model.getDateFormat(model.user.createAt),
                    modified: false),
              ),
              Divider(height: 0),
            ],
          );
        }));
  }

  Widget _buildTralling({required String text, bool modified = true}) {
    return SizedBox(
      width: 280,
      child: Row(
        children: [
          Expanded(
              child: Text(text,
                  textAlign: TextAlign.end,
                  overflow: TextOverflow.ellipsis,
                  style: TextStyle(fontSize: 16))),
          SizedBox(width: 10),
          modified ? Icon(Icons.chevron_right) : SizedBox(width: 10)
        ],
      ),
    );
  }

  // 构建头像项
  _bildAvatarUpdateItem(title, text, onTap, IndividualProvider model) {
    return Material(
      // Material 组件提供触摸反馈的上下文
      color: Colors.white,
      child: InkWell(
        // 控制水波纹颜色和形状
        splashColor: Colors.grey.withValues(alpha: 0.2),
        borderRadius: BorderRadius.circular(8),
        onTap: onTap,
        child: Padding(
          // 通过 Padding 模拟 ListTile 的内边距
          padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
          child: Row(
            children: [
              Text(title,
                  style: TextStyle(
                      fontWeight: FontWeight.w500,
                      fontSize: 20,
                      color: Colors.black)),
              Spacer(),
              GestureDetector(
                  onTap: () => model.navToAvatar(),
                  child: SquareAvatar(imageSource: text, size: 80)),
              SizedBox(height: 80, child: Icon(Icons.chevron_right)),
              SizedBox(width: 6),
            ],
          ),
        ),
      ),
    );
  }
}
